// merchant//merchants/jiuCardDetail/index.js
const app = getApp();
const utils = require('../../../utils/util.js');
const pay = require('../../utils/pay.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    typeCard: 0,//4玖玖卡5福鲤卡
    showModel: false,
    CurrentPage: 1,
    loadingMoreHidden: 1,
    keyword: '',
    cid: '',
    cardItemInfo: {},
    qrCode: '',
    merList: [],
    latitude: 0,
    longitude: 0,
    platform: '',
    inputMoney: 0,
  },

  toBuyJJCard: function () {
    let that = this;
    let cardItemInfo = that.data.cardItemInfo;
    wx.request({
      url: app.globalData.host + '/api/product/GetCardProduct?cardType=4',
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data.length > 0) {
            let jjkList = res.data.Data;
            let pid = jjkList[0].Pid;
            wx.request({
              url: app.globalData.host + '/api/Store/Product?pid=' + pid + '&isExtend=1',
              success: function (res2) {
                if (res2.data.Code == 0) {
                  var p = res2.data.Data;
                  var seltoid = 0;
                  if (p.Extend && p.Extend.AddUid > 0) {
                    seltoid = p.Extend.AddUid;
                  }
                  wx.navigateTo({
                    url: '/pages/productDetails/index?pid=' + pid + '&cardType=4&seltoid=' + seltoid + '&saleUid=' + cardItemInfo.SaleUid,
                  })
                }
              }
            })

          }
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  toRecords: function () {
    wx.navigateTo({
      url: '../jiuCardRecords/index?cid=' + this.data.cid + '&cardItemId=' + this.data.cardItemInfo.CardItemId,
    })
  },

  //安卓手机玖玖卡充值
  submitAccountBalanceOrder: function () {
    let that = this;
    if (!that.checkMoney(that.data.inputMoney)) {
      wx.showModal({
        title: '提示',
        content: '请输入50-9900整数',
        showCancel: false,
      })
      return
    }
    wx.showLoading({
      title: '充值中',
      mask: true
    })
    wx.request({
      url: app.globalData.host + '/api/order/SubmitBalanceOrder',
      data: {
        uid: app.globalData.userInfo.Uid,
        count: that.data.inputMoney / 50
      },
      method: 'POST',
      success: function (res) {
        wx.hideLoading();
        if (res.data.Code == 0) {
          pay.wxcashpay(app, res.data.Data.Oid, '/merchant/merchants/jiuCardDetail/index', app.globalData.userInfo.Uid);
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  getCardItemDetail: function (cid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCardUser?cid=' + cid,
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data) {
            that.getMerList(res.data.Data.CardItemId);
            let cardItemInfo = res.data.Data;
            cardItemInfo.ExpireTime = cardItemInfo.ExpireTime.substr(0, 10);
            that.setData({
              cardItemInfo: cardItemInfo
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 帮好友充值
  toRechargeOthers: function () {
    wx.navigateTo({
      url: '/fenxiao/rechargeOther/index?isJiuCard=1',
    })
  },

  // 商家列表
  getMerList: function (cardItemId) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/staff/GetListByConfig',
      data: {
        cardItemId: cardItemId,
        storeAppId: app.globalData.storeAppId,
        latitude: that.data.latitude,
        longitude: that.data.longitude,
        keyword: that.data.keyword,
        page: that.data.CurrentPage
      },
      method: 'POST',
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data.Items.length > 0) {
            let merList = res.data.Data.Items;
            if (that.data.CurrentPage != 1) {
              let preList = that.data.merList;
              merList = preList.concat(merList);
            }
            for (let i = 0; i < merList.length; i++) {
              merList[i].Avater = utils.formatAvater(merList[i].Avater, 'https://static.711688.net.cn/upload/card/source/');
              if (merList[i].Distance >= 1000) {
                merList[i].Distances = parseFloat(merList[i].Distance / 1000).toFixed(2);
              }
            }
            that.setData({
              merList: merList,
              loadingMoreHidden: res.data.Data.TotalPages
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },


  // 玖玖卡使用说明
  jiujiuDetail: function (e) {
    let url = 'jiujiuCardDesc.html'
    let typeCard = this.data.typeCard;
    let type = 1;
    if (typeCard == 5) {
      type = 2
    }
    wx.navigateTo({
      url: '/pages/h5page/index?url=' + url + '&type=' + type,
    })
  },

  openMap: function (e) {
    let latitude = e.currentTarget.dataset.latitude;
    let longitude = e.currentTarget.dataset.longitude;
    let company = e.currentTarget.dataset.company;
    wx.showModal({
      title: '提示',
      content: '是否打开地图导航',
      success: function (res) {
        if (res.confirm) {
          wx.openLocation({
            latitude: latitude,
            longitude: longitude,
            scale: 28,
            name: "点击右边打开地图导航",
            address: company
          })
        }
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.latitude) {
      this.data.latitude = options.latitude;
      this.data.longitude = options.longitude;
    }
    if (options) {
      this.data.cid = options.cid;
      this.getLocation(options.cid);
      this.setData({
        typeCard: options.cardType
      })
    }

    this.setData({
      platform: app.globalData.platform
    })
  },
  // 获取地理位置
  getLocation: function (cid) {
    let that = this;
    utils.getLocation((location)=>{
     that.data.latitude = location.lat;
     that.data.longitude = location.lng;
     that.getCardItemDetail(cid);
    },app);   
  },
  
  loadMore: function () {
    let that = this;
    if (that.data.loadingMoreHidden > 1 && that.data.CurrentPage < that.data.loadingMoreHidden) {
      var currPage = that.data.CurrentPage;
      that.setData({
        CurrentPage: currPage + 1
      })
      that.getMerList(that.data.cardItemInfo.CardItemId);
    } else {
      return
    }
  },

  handleInput: function (e) {
    this.setData({
      keyword: e.detail.value
    })
  },

  handleConfirm: function () {
    this.setData({
      CurrentPage: 1,
      merList: []
    })
    this.getMerList(this.data.cardItemInfo.CardItemId);
  },

  handleBlur: function () {
    this.setData({
      CurrentPage: 1,
      merList: []
    })
    this.getMerList(this.data.cardItemInfo.CardItemId);
  },

  showModel: function () {
    this.setData({
      showModel: !this.data.showModel
    })
  },

  bindInputValue: function (e) {
    this.setData({
      inputMoney: parseFloat(e.detail.value)
    })
  },

  checkInput: function () {
    if (!this.checkMoney(this.data.inputMoney)) {
      wx.showModal({
        title: '提示',
        content: '请输入50-9900整数',
        showCancel: false,
      })
      return
    }
  },

  //金额验证
  checkMoney: function (obj) {
    if (!obj) {
      return false;
    } else {
      return typeof obj === 'number' && obj % 50 === 0;//是整数，则返回true，否则返回false
    }
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
 * 用户点击右上角分享
 */
  // onShareAppMessage: function (res) {
  //   var that = this;
  //   return {
  //     title: '玖玖卡福鲤卡核销',
  //     path: "/merchant/share/jiuCardCheck/index?cid=" + that.data.cid + '&cardtype='+ that.data.typeCard +'&clientuid=' + app.globalData.userInfo.Uid,
  //     success: function (res) {
  //       wx.showModal({
  //         title: '提示',
  //         content: '转发成功',
  //         showCancel: false
  //       })
  //     }
  //   }
  // }
})